import http, {HttpOptions, HttpResponse, Page} from "../http"

import request from "../request";

// 响应接口
export interface GetPostListRes extends HttpResponse{
    code: number;
    msg: string;
    data: {
        total: number;
        size: number;
        current: number;
        records: {
            id: number;
            avatar: string;
            nickName: string;
            type: string;
            content: string;
            time: string;
            file: Record<string, unknown>[];
            likeNum: number;
            commentNum: number;
            likesId: number[]
        }[];
    };
    timestamp: number;
    now: string;
}


/**
 * 获取已公布的帖子
 * @return {Promise<GetPostListRes>}
 * @param {Page} data
 */
export function getNeighborhood(data: Page) : Promise<GetPostListRes>{
    const config: HttpOptions = {
        msg: false,
        token: true,
        load: false,
    }
    return http.get(`/post?page=${data.page}&size=${data.size}`, config)
}




export interface UploadPostVideoParam {
    user: number | string;
    postId: number | string;
    Hash: string;
    chunkHash: string;
    originFileName: string;
    video: Blob;
}

// 响应接口
export interface UploadPostVideoRes {
    code: number;
    msg: string;
    data: string;
    timestamp: number;
    now: string;
}

/**
 * 上传视频
 * @returns
 * @param {UploadPostVideoParam} data
 * @param {Blob} data.video
 * @param {string} data.originFileName
 * @param {string} data.Hash
 * @param {string} data.chunkHash
 * @param {string | number} data.postId
 * @param {string | number} data.user
 */
export function uploadPostVideo(data: FormData): Promise<UploadPostVideoRes> {
    const config: HttpOptions = {
        msg: false,
        token: true,
        load: false,
    }
    return http.post(`/post/video`, data, config);
}

// 参数接口
export interface AddPostParams {
    content?: string;
    type?: string;
    status?: number;
    address?: string;
    latitude?: number;
    updateBy?: number;
}

// 响应接口
export interface AddPostRes {
    code: number;
    msg: string;
    data: string;
    timestamp: number;
    now: string;
}


/**
 * 新增帖子
 * @param {object} params PostDto
 * @param {string} params.content
 * @param {string} params.type
 * @param {number} params.status
 * @param {string} params.address
 * @param {number} params.latitude
 * @param {number} params.updateBy
 * @returns
 */

export function addPost(params: AddPostParams): Promise<AddPostRes> {
    const config: HttpOptions = {
        msg: false,
        token: true,
        load: false,
    }
    return http.post(`/post`, params, config);
}



// 参数接口
export interface ModifyPostParams {
    id?: number;
    content?: string;
    type?: string;
    status?: number;
    likes?: number;
    interest?: number;
    comment?: number;
    forward?: number;
    ossId?: string;
    address?: string;
    latitude?: number;
    updateBy?: number;
}

// 响应接口
export interface ModifyPostRes {
    code: number;
    msg: string;
    data: string;
    timestamp: number;
    now: string;
}

/**
 * 修改帖子
 * @param {object} params PostDto
 * @param {number} params.id
 * @param {string} params.content
 * @param {string} params.type
 * @param {number} params.status
 * @param {number} params.likes
 * @param {number} params.interest
 * @param {number} params.comment
 * @param {number} params.forward
 * @param {string} params.ossId
 * @param {string} params.address
 * @param {number} params.latitude
 * @param {number} params.updateBy
 * @returns
 */
export function modifyPost(params: ModifyPostParams): Promise<ModifyPostRes> {
    const config: HttpOptions = {
        msg: false,
        token: true,
        load: false,
    }
    return http.put(`/post`, params, config);
}

// 响应接口
export interface DeletePostRes {
    code: number;
    msg: string;
    data: string;
    timestamp: number;
    now: string;
}


// 参数接口
export interface SavePostParams {
    id?: number;
    content?: string;
    type?: string;
    status?: number;
    likes?: number;
    interest?: number;
    comment?: number;
    forward?: number;
    ossId?: string;
    address?: string;
    latitude?: number;
    updateBy?: number;
    page?: number;
    size?: number;
}

// 响应接口
export interface SavePostRes {
    code: number;
    msg: string;
    data: string;
    timestamp: number;
    now: string;
}

/**
 * 保存帖子
 * @param {object} params 帖子实体类
 * @param {number} params.id 帖子id
 * @param {string} params.content 帖子正文内容
 * @param {string} params.type 帖子分类类型
 * @param {number} params.status 状态: 0-删除 1-正常 2-未发布
 * @param {number} params.likes 点赞数量
 * @param {number} params.interest 浏览数量
 * @param {number} params.comment 评论数量
 * @param {number} params.forward 转发数量
 * @param {string} params.ossId 文件存储ID
 * @param {string} params.address 地理位置地址
 * @param {number} params.latitude 纬度坐标
 * @param {number} params.updateBy 最后更新人ID
 * @param {number} params.page 分页页码
 * @param {number} params.size 每页数量
 * @returns
 */
export function savePost(params: SavePostParams): Promise<SavePostRes> {
    return http.put(`/post/save`, params);
}


/**
 * 删除帖子
 * @param {string} id
 * @param {string} user
 * @returns
 */
export function deletePost(id: number, user: number): Promise<DeletePostRes> {
    return http.delete(`/post?id=${id}&user=${user}`);
}



// 参数接口
export interface GetUserPostParams {
    page?: number | string,
    size?: number | string,
    updateBy?: number;
}

// 响应接口
export interface GetUserPostRes {
    code: number;
    msg: string;
    data: {
        total: number;
        size: number;
        current: number;
        records: {
            id: number;
            type: string;
            content: string;
            file: {
                id: number;
                clientUrl: string;
            }[];
        }[];
    };
    timestamp: number;
    now: string;
}

/**
 * 获得用户未发布的表信息
 * @param {object} params PostDto
 * @param {number} params.updateBy
 * @returns
 */
export function getUserPost(params: GetUserPostParams): Promise<GetUserPostRes> {
    const config: HttpOptions = {
        msg: false,
        token: true,
        load: false,
    }
    return http.post(`/post/userPost`, params, config);
}

// 响应接口
export interface UploadPostImgRes {
    code: number;
    msg: string;
    data: string;
    timestamp: number;
    now: string;
}

/**
 * 上传图片
 * @returns
 * @param data
 * @param {Blob[]} data.img
 * @param {string | number} data.postId
 * @param {string | number} data.user
 */
export function uploadPostImg(data: FormData): Promise<UploadPostImgRes> {
    const config: HttpOptions = {
        msg: true,
        token: true,
        load: false,
    }
    return http.post(`/post/img`,data,config);
}

// 响应接口
export interface GetUploadVideoStatusRes {
    code: number;
    msg: string;
    data: {
        url: string;
        hash: string;
        currentChunk: string;
        nextChunk: string;
    };
    timestamp: number;
    now: string;
}

/**
 * 获取视频上传状态
 * @param {string} hash 整个文件的hash值
 * @param {string} user 上传的用户的id
 * @param {string} postId 帖子id
 * @returns
 */
export function getUploadVideoStatus(hash: string, user: number, postId: number): Promise<GetUploadVideoStatusRes> {
    const config: HttpOptions = {
        msg: false,
        token: true,
        load: false,
    }
    return http.get(`/post/videoStatus/${hash}?user=${user}&postId=${postId}`, config);
}


// 响应接口
export interface DeleteEditPostRes {
    code: number;
    msg: string;
    data: Record<string, unknown>;
    timestamp: number;
    now: string;
}

/**
 * 删除编辑中的帖子
 * @param {string} id
 * @param {string} user
 * @returns
 */
export function deleteEditPost(id: number, user: number): Promise<DeleteEditPostRes> {
    const config: HttpOptions = {
        msg: true,
        token: true,
        load: false,
    }
    return http.delete(`/post/edit?id=${id}&user=${user}`, config);
}


// 响应接口
export interface DelPostImgRes {
    code: number;
    msg: string;
    data: string;
    timestamp: number;
    now: string;
}

/**
 * 删除帖子的文件
 * @param {string} postId 帖子ID
 * @param {string} img 图片ID
 * @param {string} user 用户ID
 * @returns
 */
export function delPostImg(postId: number, img: number, user: number): Promise<DelPostImgRes> {
    const config: HttpOptions = {
        msg: true,
        token: true,
        load: false,
    }
    return http.delete(`/post/delImg?postId=${postId}&img=${img}&user=${user}`, config);
}

// 响应接口
export interface StopUploadVideoRes {
    code: number;
    msg: string;
    data: {
        url: string;
        hash: string;
        currentChunk: string;
        nextChunk: string;
    };
    timestamp: number;
    now: string;
}

/**
 * 停止上传视频
 * @param {string} hash 视频hash
 * @param {string} user 用户ID
 * @returns
 */
export function stopUploadVideo(hash: string, user: number): Promise<StopUploadVideoRes> {
    const config: HttpOptions = {
        msg: true,
        token: true,
        load: false,
    }
    return http.delete(`/post/stopUploadVideo?hash=${hash}&user=${user}`,config);
}


// 参数接口
export interface AddLikePostParams {
    id?: number;
    sourceId?: number;
    founder?: number;
    status?: number;
}

// 响应接口
export interface AddLikePostRes {
    code: number;
    msg: string;
    data: string;
    timestamp: number;
    now: string;
}

/**
 * 增加点赞
 * @param {object} params 点赞表
 * @param {number} params.id
 * @param {number} params.sourceId 帖子id
 * @param {number} params.founder 点赞用户id
 * @param {number} params.status 点赞状态 0 无效点赞 1 点赞
 * @returns
 */
export function addLikePost(params: AddLikePostParams): Promise<AddLikePostRes> {
    const config: HttpOptions = {
        msg: false,
        token: true,
        load: false,
    }
    return http.post(`/like_post`, params, config);
}

// 响应接口
export interface DeleteLikePostRes {
    code: number;
    msg: string;
    data: string;
    timestamp: number;
    now: string;
}

/**
 * 删除点赞
 * @param {string} id 点赞id
 * @returns
 */
export function deleteLikePost(id: number): Promise<DeleteLikePostRes> {
    const config: HttpOptions = {
        msg: false,
        token: true,
        load: false,
    }
    return http.delete(`/like_post/${id}`,config);
}

// 响应接口
export interface DeleteLikePost_1Res {
    code: number;
    msg: string;
    data: string;
    timestamp: number;
    now: string;
}

/**
 * 删除点赞2
 * @param {string} postId 帖子id
 * @param {string} userId 用户id
 * @returns
 */
export function deleteLikePost_1(postId: number, userId: number): Promise<DeleteLikePost_1Res> {
    const config: HttpOptions = {
        msg: false,
        token: true,
        load: false,
    }
    return http.delete(`/like_post/?postId=${postId}&userId=${userId}`, config);
}

// 响应接口
export interface GetPostByIdRes {
    code: number;
    msg: string;
    data: {
        id: number;
        avatar: string;
        nickName: string;
        type: string;
        content: string;
        time: string;
        file: {
            id: number;
            clientUrl: string;
            type: string;
        }[];
        likeNum: number;
        likesId: Record<string, unknown>[];
        commentNum: number;
        comments: {
            id: number;
            nickName: string;
            avatar: string;
            fileClient: string;
            stepId: number;
            content: string;
            time: string;
            commentLike: number;
            comment: number;
        }[];
    };
    timestamp: number;
    now: string;
}

/**
 * 获得已公布帖子详情
 * @param {string} id 帖子ID
 * @returns
 */
export function getPostById(id: number): Promise<GetPostByIdRes> {
    const config: HttpOptions = {
        msg: false,
        token: true,
        load: false,
    }
    return http.get(`/post/${id}`, config);
}

// 响应接口
export interface AddCommentRes {
    code: number;
    msg: string;
    data: string;
    timestamp: number;
    now: string;
}

/**
 * 增加评论
 * @returns
 * @param data
 * @param data.postId 帖子ID
 * @param data.userId 用户ID
 * @param data.content 评论内容
 * @param data.img 评论图片
 * @param data.step 父评论ID
 */
export function addComment(data: FormData): Promise<AddCommentRes> {
    const config: HttpOptions = {
        msg: false,
        token: true,
        load: false,
    }
    return http.post(`/comment`,data, config);
}

// 参数接口
export interface AddLikeCommentParams {
    id?: number;
    sourceId?: number;
    founder?: number;
    status?: number;
    commentId?: number;
    type?: number;
}

// 响应接口
export interface AddLikeCommentRes {
    code: number;
    msg: string;
    data: string;
    timestamp: number;
    now: string;
}

/**
 * 增加点赞评论
 * @param {object} params 点赞表
 * @param {number} params.id
 * @param {number} params.sourceId 帖子id
 * @param {number} params.founder 点赞用户id
 * @param {number} params.status 点赞状态 0 无效点赞 1 点赞
 * @param {number} params.commentId 评论id
 * @param {number} params.type 点赞类型 0 帖子 1 评论
 * @returns
 */
export function addLikeComment(params: AddLikeCommentParams): Promise<AddLikeCommentRes> {
    const config: HttpOptions = {
        msg: false,
        token: true,
        load: false,
    }
    return http.post(`/like_post/comment`, params, config);
}

// 响应接口
export interface DeleteLikeCommentRes {
    code: number;
    msg: string;
    data: string;
    timestamp: number;
    now: string;
}

/**
 * 删除评论点赞1
 * @param {string} commentId
 * @param {string} userId
 * @returns
 */
export function deleteLikeComment(commentId: number, userId: number): Promise<DeleteLikeCommentRes> {
    const config: HttpOptions = {
        msg: false,
        token: true,
        load: false,
    }
    return http.delete(`/like_post/comment?commentId=${commentId}&userId=${userId}`,config);
}


// 响应接口
export interface GetUserAllPostRes {
    code: number;
    msg: string;
    data: {
        total: number;
        size: number;
        current: number;
        records: {
            id: number;
            avatar: string;
            nickName: string;
            type: string;
            content: string;
            time: string;
            file: {
                id: number;
                clientUrl: string;
                type: string;
            }[];
            likeNum: number;
            likesId: Record<string, unknown>[];
            commentNum: number;
            status: number;
            message: string;
        }[];
    };
    timestamp: number;
    now: string;
}

/**
 * 获得用户所有帖子
 * @param {string} userId 用户ID
 * @param {string} page 页码
 * @param {string} size 每页数量
 * @returns
 */
export function getUserAllPost(userId: number, page: number, size: number): Promise<GetUserAllPostRes> {
    const config:HttpOptions  = {
        msg: false,
        token: true,
        load: false,
    }
    return http.get(`/post/userPost?userId=${userId}&page=${page}&size=${size}`,config);
}